Repository navigation
[bug] Make CallableModel validators cheap when rejecting a value - #294
Merged
Merged
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #294 +/- ##
==========================================
+ Coverage 93.88% 93.89% +0.01%
==========================================
Files 203 203
Lines 22706 22719 +13
Branches 1491 1491
==========================================
+ Hits 21318 21333 +15
+ Misses 1100 1099 -1
+ Partials 288 287 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Contributor
ptomecek
force-pushed
the
feature/cheap-callable-reject
branch
2 times, most recently
from
November 11, 2026 00:22
c2756d2 to
5b2cd62
Compare
ptomecek
force-pushed
the
feature/cheap-callable-reject
branch
5 times, most recently
from
November 11, 2026 01:02
5a8193e to
1a1581c
Compare
hintse
approved these changes
Nov 11, 2026
CallableModelGeneric._validate_callable_model_generic_type is a wrap
validator, so pydantic runs it on every rejected branch of a union like
`CallableModelGenericType[Ctx, R] | R`. The old rejection message
formatted the whole input value (`f"{m} ..."`) just to build an error
that pydantic throws away before trying the next branch. For values
expensive to repr this dominated construction -- ~36 ms of a ~37 ms
build in one case.
Drop the value from the message, and do the same for the other coercion
validators that formatted the whole value in their reject path (enums,
arrow, exprtk, polars, numpy ndarray, pyobjectpath). pydantic already
appends input_value=/input_type= to the error (lazily, so the discarded
branch pays nothing), so the messages also stop restating the type --
including the redundant `got {type(...)}` in the narwhals, arrow-table,
and file-publisher validators. Exception type stays ValueError so union
fallback keeps working.
Signed-off-by: Pascal Tomecek <40371786+ptomecek@users.noreply.github.com>
ptomecek
force-pushed
the
feature/cheap-callable-reject
branch
from
November 11, 2026 14:23
1a1581c to
f325e77
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
_validate_callable_model_generic_typeis a wrap validator, so pydantic runs it on every rejected branch of a union likeCallableModelGenericType[Ctx, R] | R. When a plainRis passed, the old rejection message formatted the whole value (f"{m} ...") just to build an error that pydantic throws away before trying the next branch. For values that are expensive toreprthis dominated construction — ~36 ms of a ~37 ms build in one case.The message now carries no value, and I applied the same fix to the other coercion validators that formatted the whole value in their reject path.
Performance-sensitive locations (formatted the whole input value on a rejected branch):
callable.py—_validate_callable_model_generic_typeenums.py—Enum.validateexttypes/arrow.py—ArrowSchema._validate,PyArrowDatatype._validateexttypes/exprtk.py—ExprTkExpression._validateexttypes/polars.py—_PolarsExprPydanticAnnotation._validateexttypes/pydantic_numpy/ndarray.py—NDArray._validateexttypes/pyobjectpath.py—PyObjectPath._validatepydantic already appends
input_value=/input_type=to the error (rendered lazily, so the discarded branch pays nothing), so the messages also stop restating the type. That drops a redundantgot {type(...)}from a few validators that weren't a perf problem but duplicated pydantic:exttypes/narwhals.py,exttypes/arrow.py(Table), andpublishers/file.py.The failing value isn't lost: on a genuine failure pydantic's
input_value=/input_type=still show it.Measurement
Union field given an
Rwhosereprrenders a 3,000×72 frame: ~13 ms → ~3 µs per construction.Type of Change
Checklist
make lint)make test)